import { EventName, eventsOnLoad, preloadEvent } from '../EventUtil';
/*
 * @Author: Crazy_Debug yangcheng960813@163.com
 * @Date: 2023-04-22 20:27:20
 * @LastEditTime: 2024-07-17 22:07:38
 * @LastEditors: yayalee yangcheng960813@163.com
 * @Description: 
 * @FilePath: \unfinished-challenges\assets\core_cc\widget\AdapterCanvas.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import { Component, ResolutionPolicy, _decorator, view } from "cc";

const { ccclass, property, menu, disallowMultiple } = _decorator;

/**
 * 分辨率适配组件，保证设计分辨率区域全部都能显示
 */
@ccclass("AdapterCanvas")
@eventsOnLoad()
@disallowMultiple
@menu("Core_cc/AdapterCanvas")
export default class AdapterCanvas extends Component {

    protected onLoad(): void {
        this.adapt();
    }

    @preloadEvent(EventName.RESIZE)
    private adapt(): void {
        // 设置适配
        const designResolution = view.getDesignResolutionSize();
        const visibleSize = view.getVisibleSize();
        // 场景适配
        if (visibleSize.width / visibleSize.height < designResolution.width / designResolution.height) {
            view.setDesignResolutionSize(designResolution.width, designResolution.height, ResolutionPolicy.FIXED_WIDTH);
        } else {
            view.setDesignResolutionSize(designResolution.width, designResolution.height, ResolutionPolicy.FIXED_HEIGHT);
        }
    }
}
